﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SQLDLL;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraEditors;
using DLL;
using NursingFormSystem;

namespace NursingDocumentInfoSystem
{
    public partial class NursingMeasure : Form
    {
        public string Did = "";
        public string TTT = "";
        public NursingMeasure(string text, string depatrmentid)
        {
            InitializeComponent();
            Did = depatrmentid;
            TTT = text;
            richE_ContentInfor.Text = text;
            getMeasure();
            StartPosition = FormStartPosition.CenterScreen;
        }

        void getMeasure()
        {
            string sql = string.Format(" SELECT * FROM [BNNursingDocsSystem_Develop].[dbo].[NursingMeasure] where ifuse = {0} AND DepartmentID = '{1}' ", 1, Did);
            if (cmbTemplateType.SelectedIndex == 1)
                sql = string.Format(" SELECT * FROM [BNNursingDocsSystem_Develop].[dbo].[NursingMeasure] where ifuse = {0} ", 1);
            DataTable measure = SqlHelper.ExecuteDataset(sql, CommandType.Text).Tables[0];
            measure.Columns.Add("勾选", System.Type.GetType("System.Boolean"));
            measure.Columns["勾选"].SetOrdinal(0);
            gridMeasure.DataSource = measure;
            gridView2.Columns["ID"].Visible = false;
            gridView2.Columns["ifuse"].Visible = false;
            gridView2.Columns["DepartmentID"].Visible = false;
            gridView2.Columns["text2"].Visible = false;
            gridView2.Columns["text1"].Caption = "标题";
            gridView2.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;
            gridView2.OptionsCustomization.AllowFilter = false;
            gridView2.OptionsSelection.MultiSelect = true;
            gridView2.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
            gridView2.OptionsBehavior.Editable = true;
            gridView2.OptionsCustomization.AllowSort = false;
            gridView2.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            gridView2.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            gridView2.FocusRectStyle = DrawFocusRectStyle.RowFocus;
            gridView2.OptionsSelection.EnableAppearanceFocusedCell = false;
            gridView2.OptionsMenu.EnableColumnMenu = false;
            gridView2.OptionsView.ColumnAutoWidth = false;
            gridView2.HorzScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibility.Always;
            gridView2.VertScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibility.Always;
            gridView2.CustomDrawColumnHeader += GridView2_CustomDrawColumnHeader;
            gridView2.Click += GridView2_Click;
            gridView2.Columns[0].Caption = " ";
            gridView2.Columns["text1"].Width = 240;
            gridView2.Columns[0].Width = 30;
            gridView2.Columns["text1"].OptionsColumn.AllowEdit = false;
        }
        private void GridView2_Click(object sender, EventArgs e)
        {
            Point pt = gridMeasure.PointToClient(MousePosition);

            GridHitInfo info = gridView2.CalcHitInfo(pt);
            if (info.Column == null)
            {
                return;
            }
            string value;
            int Checked = 0;
            if (!info.InRowCell && info.Column.AbsoluteIndex == 0)//Index判断用于哪一列的列头
            {
                for (int i = 0; i < gridView2.RowCount; i++)
                {
                    value = gridView2.GetDataRow(i)["勾选"].ToString();
                    if (value == "True")
                    {
                        Checked++;
                    }
                }
                var checkedCountIs0 = (Checked == 0);
                for (int i = 0; i < gridView2.RowCount; i++)
                {
                    gridView2.SetRowCellValue(i, gridView2.Columns[0], checkedCountIs0);
                }
            }
        }

        private void GridView2_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
        {
            GridView _view = sender as GridView;
            Draw.DrawHeaderCheckBox(gridView2, CheckItem, gcCheckFieldName, e);
        }
        RepositoryItemCheckEdit CheckItem = new RepositoryItemCheckEdit();
        const string gcCheckFieldName = "勾选";

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            TTT = richE_ContentInfor.Text;
            Close();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            string value = "";
            int Checked = 0;
            for (int i = 0; i < gridView2.RowCount; i++)
            {
                value = gridView2.GetDataRow(i)["勾选"].ToString();
                if (value == "True")
                {
                    Checked++;
                }
            }
            if (Checked == 0)
            {
                XtraMessageBox.Show("没有勾选任何条目");
                return;
            }
            else
            {
                string texttemp = "";
                for (int i = 0; i < gridView2.RowCount; i++)
                {
                    value = gridView2.GetDataRow(i)["勾选"].ToString();
                    if (value == "True")
                    {
                        texttemp += gridView2.GetDataRow(i)["text2"].ToString();
                        texttemp += "\r\n";
                    }
                }
                richE_ContentInfor.Text += texttemp;
            }
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            NursingMeasure2 xx = new NursingMeasure2("-1", Did);
            xx.ShowDialog();
            if (xx.DialogResult == DialogResult.OK)
            {
                getMeasure();
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string value = "";
            int Checked = 0;
            for (int i = 0; i < gridView2.RowCount; i++)
            {
                value = gridView2.GetDataRow(i)["勾选"].ToString();
                if (value == "True")
                {
                    Checked++;
                    if (Checked > 1)
                    {
                        XtraMessageBox.Show("暂时仅支持修改单独条目");
                        return;
                    }
                }
            }
            if (Checked == 0)
            {
                XtraMessageBox.Show("没有勾选任何条目");
                return;
            }
            else
            {
                for (int i = 0; i < gridView2.RowCount; i++)
                {
                    value = gridView2.GetDataRow(i)["勾选"].ToString();
                    if (value == "True")
                    {
                        string id = gridView2.GetDataRow(i)["ID"].ToString();
                        NursingMeasure2 xx = new NursingMeasure2(id, Did);
                        xx.ShowDialog();
                        if (xx.DialogResult == DialogResult.OK)
                        {
                            getMeasure();
                        }
                    }
                }
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (cmbTemplateType.SelectedIndex == 1) 
            {
                XtraMessageBox.Show("不能删除全院的模板，请切换到科室模板");
                return;
            }
            string value = "";
            int Checked = 0;
            for (int i = 0; i < gridView2.RowCount; i++)
            {
                value = gridView2.GetDataRow(i)["勾选"].ToString();
                if (value == "True")
                {
                    Checked++;
                }
            }
            if (Checked == 0)
            {
                XtraMessageBox.Show("没有勾选任何条目");
                return;
            }
            else
            {
                DialogResult dialogResult = XtraMessageBox.Show("确定要删除吗？", "删除询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.OK)
                {
                    string id = "";
                    string sql = "";
                    int resCount = 0;
                    for (int i = 0; i < gridView2.RowCount; i++)
                    {
                        value = gridView2.GetDataRow(i)["勾选"].ToString();
                        if (value == "True")
                        {
                            id = gridView2.GetDataRow(i)["ID"].ToString();
                            sql = string.Format(" update [BNNursingDocsSystem_Develop].[dbo].[NursingMeasure]  set ifuse ='0' where ID = '{0}' AND DepartmentID = '{1}' ", id, Did);
                            resCount = SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                        }
                    }
                    XtraMessageBox.Show("删除成功！");
                    getMeasure();
                }
            }
        }

        private void cmbTemplateType_SelectedIndexChanged(object sender, EventArgs e)
        {
            getMeasure();
        }
    }
}
